/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.acd;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

import junit.framework.TestCase;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.apache.commons.io.filefilter.SuffixFileFilter;

import net.bioteam.appweb.TestConfig;



public class AcdParserTest extends TestCase {
	public AcdParserTest(String string)
	{
		super(string);
	}

	public void setUp() throws Exception {

	}

	public void tearDown() throws Exception {

	}

	public static void main(String args[]) {
		AcdParserTest test = new AcdParserTest("testSingleAcd");
		junit.textui.TestRunner.run(test);
	}
	/**
	 * Make sure out antlr grammar works
	 * @throws Exception
	 */
	public void testAcdParse()  {

		File acddir = new File(TestConfig.topdir+"/emboss/acd");
		File files[] = acddir.listFiles((FileFilter)new SuffixFileFilter(".acd"));
		int count = 0;
		for (File acdfile : files) {
			try {
				ANTLRInputStream input = new ANTLRInputStream(
						new FileInputStream(acdfile));
				AcdLexer lexer = new AcdLexer(input);
				CommonTokenStream tokens = new CommonTokenStream(lexer);
				AcdParser parser = new AcdParser(tokens);
				AcdParser.program_return ret = parser.program();
				// program tree
				CommonTree tree = (CommonTree) ret.getTree();
				// application parameter
				CommonTree appTree = (CommonTree) tree.getChild(0);

				// should be application name;
				String appName = appTree.getChild(1).getText();
				System.err.println(appName+" passed ");


			} catch (Exception e) {
				System.err.println("error for " + acdfile.getName());
				e.printStackTrace();
				fail("error for " + acdfile.getName());
//				if (acdfile.getName().equals("ehmmpfam.acd") || acdfile.getName().equals("ehmmsearch.acd"))
//				{
//					//these two program can not be parsed.
//					continue;
//				} else
//				{
//					
//				}
			}
		}

	}
	
	public void testSingleAcd()  {

		File acdfile = new File(TestConfig.topdir+"/emboss/acd/emira.acd");
		
			try {
				ANTLRInputStream input = new ANTLRInputStream(
						new FileInputStream(acdfile));
				AcdLexer lexer = new AcdLexer(input);
				CommonTokenStream tokens = new CommonTokenStream(lexer);
				AcdParser parser = new AcdParser(tokens);
				AcdParser.program_return ret = parser.program();
				// program tree
				CommonTree tree = (CommonTree) ret.getTree();
				// application parameter
				CommonTree appTree = (CommonTree) tree.getChild(0);

				// should be application name;
				String appName = appTree.getChild(1).getText();
				System.out.println(appName+" passed ");
				


			} catch (Exception e) {
				System.err.println("error for " + acdfile.getName());
				e.printStackTrace();
				fail("error for " + acdfile.getName());
//				if (acdfile.getName().equals("ehmmpfam.acd") || acdfile.getName().equals("ehmmsearch.acd"))
//				{
//					//these two program can not be parsed.
//					continue;
//				} else
//				{
//					
//				}
			}
		

	}
}
